package net.teamix;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.component.html.HtmlColumn;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ActionListener;
import javax.faces.event.MethodExpressionActionListener;

public class LangListBean {

	// Init
	// --------------------------------------------------------------------------------------

	private static List<List<String>> dynamicList; // Simulate fake DB.
	private static String[] dynamicHeaders; // Optional.
	private HtmlPanelGroup dynamicDataTableGroup; // Placeholder.
	public XMLService xmlService;

	// Actions
	// -----------------------------------------------------------------------------------

	private void loadDynamicList() {

		// Renew group.
		dynamicDataTableGroup = new HtmlPanelGroup();

		// Set headers (optional).
		dynamicHeaders = (String[]) xmlService.getLangList();
		List<TranslationElement> transElements = xmlService
				.getTranslationElements();
		dynamicList = new ArrayList<List<String>>();
		for (int i = 0; i < transElements.size(); i++) {
			String[] sprachListe = transElements.get(i).getSprachen();
			String[] dynListSprachen = new String[dynamicHeaders.length];
			for (int j = 0; j < dynamicHeaders.length; j++) {
				if (Arrays.asList(sprachListe).contains(dynamicHeaders[j]))
					dynListSprachen[j] = "XX";
				else
					dynListSprachen[j] = "OO";
			}
			dynamicList.add(Arrays.asList(dynListSprachen));
		}

	}

	private void populateDynamicDataTable() {

		// Create datatable.
		HtmlDataTable dynamicDataTable = new HtmlDataTable();
		dynamicDataTable.setId("translationsIDs");
		dynamicDataTable.setValueExpression("value", createValueExpression(
				"#{langListBean.dynamicList}", List.class));
		dynamicDataTable.setVar("dynamicItem");
		dynamicDataTable.setBorder(0);
		dynamicDataTable.setStyleClass("lang_table");
		// Get amount of columns.
		int columns = dynamicHeaders.length;
		int i;
		// Loop through columns.
		for (i = 0; i < columns; i++) {

			// Create column.
			HtmlColumn column = new javax.faces.component.html.HtmlColumn();

			// Create header (optional) and add to column.
			HtmlOutputText header = new HtmlOutputText();
			header.setValue(dynamicHeaders[i]);
			column.setHeader(header);
			
			// Create output and add to column.
			HtmlOutputText output = new HtmlOutputText();
			output.setValueExpression("value", createValueExpression(
					"#{dynamicItem[" + i + "]}", String.class));
			output.setValueExpression("styleClass", createValueExpression(
					"#{dynamicItem[" + i + "] == 'XX' ? 'xclass' : 'oclass'}", String.class));
			column.getChildren().add(output);

			// Add column to datatable.
			dynamicDataTable.getChildren().add(column);
		}

		dynamicDataTableGroup.getChildren().add(dynamicDataTable);
	}

	// Getters
	// -----------------------------------------------------------------------------------

	public HtmlPanelGroup getDynamicDataTableGroup() {
		// This will be called once in the first RESTORE VIEW phase.
		if (dynamicDataTableGroup == null) {
			loadDynamicList(); // Preload dynamic list.
			populateDynamicDataTable(); // Populate editable datatable.
		}

		return dynamicDataTableGroup;
	}

	public List<List<String>> getDynamicList() {
		return dynamicList;
	}

	// Setters
	// -----------------------------------------------------------------------------------

	public void setDynamicDataTableGroup(HtmlPanelGroup dynamicDataTableGroup) {
		this.dynamicDataTableGroup = dynamicDataTableGroup;
	}

	// Helpers
	// -----------------------------------------------------------------------------------

	private ValueExpression createValueExpression(String valueExpression,
			Class<?> valueType) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		return facesContext.getApplication().getExpressionFactory()
				.createValueExpression(facesContext.getELContext(),
						valueExpression, valueType);
	}

	private MethodExpression createActionExpression(String actionExpression,
			Class<?> returnType) {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		return facesContext.getApplication().getExpressionFactory()
				.createMethodExpression(facesContext.getELContext(),
						actionExpression, returnType, new Class[0]);
	}

	public XMLService getXmlService() {
		return xmlService;
	}

	public void setXmlService(XMLService xmlService) {
		this.xmlService = xmlService;
	}

	public ArrayList getDynamicHeaders() {
		ArrayList<String> array = new ArrayList(Arrays.asList(dynamicHeaders));
		return array;
	}

	public void setDynamicHeaders(String[] dynamicHeaders) {
		LangListBean.dynamicHeaders = dynamicHeaders;
	}
}